﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WingsOverSpace
{
    class Asteroid : WingsDrawableGameComponent
    {
        public Vector2 Velocity { get; set; }
        SpriteBatch spriteBatch;
        public float Rotation = 0f;

        public Asteroid(Game g, Vector2 position)
            : base(g)
        {
            Position = position;
            Velocity = new Vector2(0, 0);
            SpriteScaleFactor = 0.7f;
            SpriteHeightDivFactor = 1;
            SpriteWidthDivFactor = 1;
            Visible = false;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            Vector2 offset = new Vector2(sprite.Width / (2 * SpriteWidthDivFactor) , sprite.Height / (2 * SpriteHeightDivFactor));
            spriteBatch.Begin();
            spriteBatch.Draw(sprite, boundBox, null, Color.White, Rotation,
                new Vector2(offset.X, offset.Y), SpriteEffects.None, 0);
            /*
            spriteBatch.Draw(sprite, Position - offset, null, Color.White,
                Rotation, new Vector2(0, 0), SpriteScaleFactor, SpriteEffects.None, 0);
            */
            spriteBatch.End();
        }

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("Asteroids");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            base.LoadContent();
        } 

        public override void Update(GameTime gameTime)
        {
            
            boundBox = new Rectangle((int)(Position.X - SpriteScaleFactor * sprite.Width / (2 * SpriteWidthDivFactor)),
                (int)(Position.Y - SpriteScaleFactor * sprite.Height / (2 * SpriteHeightDivFactor)),
                (int)(SpriteScaleFactor * sprite.Width / SpriteWidthDivFactor),
                (int)(SpriteScaleFactor * sprite.Height / SpriteHeightDivFactor));


            if (toBeDeleted == true)
            {
                Visible = false;
                toBeDeleted = false;
            }

            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Position += Velocity * elapsedSeconds;
            Rotation += elapsedSeconds * 1.2f;
            //Rotation = 0;

            base.Update(gameTime);
            
        }

    }
}
